package com.wk.code.wkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.code.wkserver.entity.TechApplyRecord;
import com.wk.code.wkserver.entity.TechType;
import com.wk.code.wkserver.entity.User;
import com.wk.code.wkserver.mapper.TechApplyRecordMapper;
import com.wk.code.wkserver.mapper.UserMapper;
import com.wk.code.wkserver.request.base.BasePageRequest;
import com.wk.code.wkserver.response.admin.TechUserApplyRecordResponse;
import com.wk.code.wkserver.response.base.PageInfo;
import com.wk.code.wkserver.service.ITechApplyRecordService;
import com.wk.code.wkserver.utils.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: wangqiang
 * @create: 2021-08-26 10:11
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TechApplyRecordService extends ServiceImpl<TechApplyRecordMapper, TechApplyRecord> implements ITechApplyRecordService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void save(Integer userId, Integer sysUserId, Integer verifyStatus, String remark) {
        TechApplyRecord record = new TechApplyRecord();
        record.setUserId(userId);
        record.setSysUserId(sysUserId);
        record.setRemark(remark);
        record.setVerifyStatus(verifyStatus);
        save(record);
    }

    @Override
    public PageInfo<TechUserApplyRecordResponse> pageList(BasePageRequest request) {
        IPage<TechApplyRecord> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<TechApplyRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(TechApplyRecord::getCreateTime);
        List<TechApplyRecord> records = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            List<TechUserApplyRecordResponse> list = records.stream().map(x -> {
                TechUserApplyRecordResponse response = new TechUserApplyRecordResponse();
                User user = userMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, x.getUserId()));
                if (Objects.nonNull(user)) {
                    response.setAvatar(user.getAvatar());
                    response.setMobile(user.getMobile());
                    response.setNickname(user.getNickname());
                    response.setUserId(x.getUserId());
                    response.setGender(user.getGender());
                    response.setId(x.getId());
                    response.setCreateTime(DateUtils.localDateTime2Date(x.getCreateTime()));
                }
                return response;
            }).collect(Collectors.toList());
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;
    }
}
